<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4-1 使用组件的细节点</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div id="root">
        <div ref="hello" @click="handleClick">hello world</div>

        <counter ref="one" @change="handleChange"></counter>
        <counter ref="two" @change="handleChange"></counter>
        <div>{{total}}</div>
    </div>

    <script>
        Vue.component('counter', {
            template: '<div @click="handleClickNumber">{{number}}</div>',
            data() {
                return {
                    number: 0
                }
            },
            methods: {
                handleClickNumber() {
                    this.number++;
                    this.$emit('change')
                }
            }
        });

        var vm = new Vue({
            el: '#root',
            data: {
                total: 0
            },
            methods: {
                handleClick() {
                    // <div>hello world</div>
                    console.log(this.$refs.hello);
                    // hello world
                    alert(this.$refs.hello.innerHTML);
                },
                handleChange() {
                    console.log(this.$refs.one.number);
                    console.log(this.$refs.two.number);
                    this.total = this.$refs.one.number + this.$refs.two.number;
                }
            },
        })
    </script>
</body>

</html>